package com.raven.play.stream.test.javadsl.tutorial;

import akka.Done;
import akka.NotUsed;
import akka.japi.Pair;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import com.raven.play.stream.test.BaseTest;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletionStage;

public class $05FirstPrinciples extends BaseTest {

    @Test
    public void test1() {

        Source<Integer, NotUsed> source = Source.range(1, 10);
        // Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5));
        Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).map(it -> it * 2);
        Sink<Integer, CompletionStage<Done>> sink = Sink.foreach(it -> log.info(it.toString()));

        CompletionStage<Done> run = source.via(flow).toMat(sink, Keep.right()).run(system);

        run.thenRun(() -> {
            log.info("Finished");
            system.terminate();
        });
    }
}
